package com.test.rocketmq.consumer;

import com.test.rocketmq.bean.MessageEvent;
import org.apache.rocketmq.common.message.MessageExt;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.util.List;

/**
 * 创建事件监听02：通过@EventListener注解实现
 *
 * 消费消息
 * */
@Component
public class ConsumerService02 {

    @EventListener(classes = {MessageEvent.class})
    //@EventListener(condition = "#event.messageEvent.msgs == 'user-topic'")
    public void consumeMsgs(MessageEvent messageEvent){
        try{
            List<MessageExt> msgs = messageEvent.getMsgs();
            for(MessageExt msg : msgs ){
                System.out.println("消费消息02："+new String(msg.getBody()));
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    /*@EventListener(condition = "#event.msgs[0].topic=='user-topic' && #event.msgs[0].tags=='white'")
    public void rocketmqMsgListener(MessageEvent messageEvent){
        try{
            *//*List<MessageExt> msgs = messageEvent.getMsgs();
            for(MessageExt msg : msgs){
                System.err.println("消费消息:"+new String(msg.getBody()));
            }*//*
            System.out.println("消费成功");
        }catch (Exception e){
            e.printStackTrace();
        }
    }*/
}
